//
//  TimePickerCell.m
//
//  Created by Olof Millberg on 2010-04-09.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TimePickerCell.h"

@implementation TimePickerCell
@synthesize timeTypeButton;
@synthesize timeIntervalButton;
@synthesize deleteButton;
@synthesize enabled;
@synthesize timeType;
@synthesize timeInterval;
@synthesize startTime;
@synthesize stopTime;
@synthesize timePickerType;
@synthesize delegate;
@synthesize timeTypes;

- (void) setEnabled:(BOOL)isEnabled
{
	enabled = isEnabled;
	self.timeTypeButton.enabled = enabled;
	self.timeIntervalButton.enabled = enabled;
	
	if (enabled)
	{
		[timeTypeButton setBackgroundImage:[UIImage imageNamed:@"button_halfWidth.png"] forState:UIControlStateNormal];
		[timeIntervalButton setBackgroundImage:[UIImage imageNamed:@"button_halfWidth.png"] forState:UIControlStateNormal];
	}
	else
	{
		[timeTypeButton setBackgroundImage:nil forState:UIControlStateNormal];
		[timeIntervalButton setBackgroundImage:nil forState:UIControlStateNormal];
	}
	
	// Move the time interval button to the edge if we're not editing.
	CGRect frame = self.timeIntervalButton.frame;
	if (enabled)
		frame.size.width = 140;
	else
		frame.size.width = self.frame.size.width - frame.origin.x - 6;
	self.timeIntervalButton.frame = frame;
	
	self.deleteButton.hidden = !enabled;
}

- (void) setTimeType:(NSString *)aTimeType
{
	[timeType release];
	timeType = [aTimeType retain];
	[self.timeTypeButton setTitle:timeType forState:UIControlStateNormal];
}

- (void) setTimeInterval:(NSTimeInterval)aTimeInterval
{
	timeInterval = aTimeInterval;
	NSInteger hours = timeInterval / 3600;
	NSInteger minutes = (timeInterval - hours * 3600) / 60;
	[self.timeIntervalButton setTitle:[NSString stringWithFormat:@"%i timmar %i minuter", hours, minutes]
								 forState:UIControlStateNormal];
}

/* Handle UIActionSheet selections */
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (buttonIndex != actionSheet.cancelButtonIndex) 
	{
		self.timeType = [actionSheet buttonTitleAtIndex:buttonIndex];
		[delegate timePickerCell:self didChangeTimeType:[actionSheet buttonTitleAtIndex:buttonIndex]];
	}
	else if (timePicker)
	{
		self.timeInterval = timePicker.countDownDuration;
		[delegate timePickerCell:self didChangeTimeInterval:timePicker.countDownDuration];
		[timePicker release];
		timePicker = nil;
	}
	
	[actionSheet release];
}

/* Launch time type selection menu */
- (IBAction) showTimeTypeMenu:(id)sender
{
	UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle: @"Typ av tid:"
															 delegate:self
													cancelButtonTitle:nil
											   destructiveButtonTitle:nil
													otherButtonTitles:nil];
	for(NSString *currentTimeType in self.timeTypes)
		[actionSheet addButtonWithTitle:currentTimeType];
	
	[actionSheet addButtonWithTitle:@"Tillbaka"];
	[actionSheet setCancelButtonIndex:self.timeTypes.count];
	actionSheet.tag = ((UIButton *)sender).tag;
	[actionSheet showInView:self.window];
}

/* Launch time amount selection menu */
- (IBAction) showTimeIntervalPicker:(id)sender
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n"
															 delegate:self
													cancelButtonTitle:@"OK"
											   destructiveButtonTitle:nil
													otherButtonTitles:nil];
	actionSheet.tag = ((UIButton *)sender).tag;
	[actionSheet showInView:self.window];
	
	// Add the picker
	timePicker = [[UIDatePicker alloc] init];
	timePicker.datePickerMode = UIDatePickerModeCountDownTimer;
	timePicker.minuteInterval = 5;
	timePicker.countDownDuration = self.timeInterval;	
	[actionSheet addSubview:timePicker];
}

- (IBAction) deleteButtonClicked:(id)sender
{
	[delegate timePickerCellDeleteButtonClicked:self];
}

@end